import { defineStore } from 'pinia'
import { getUserList, addUser, updateUserStatus, deleteUser } from '../api/admin'

export const useAdminStore = defineStore('admin', {
  state: () => ({
    userList: [],
    total: 0,
    loading: false
  }),
  
  actions: {
    async fetchUserList(params) {
      this.loading = true
      try {
        const response = await getUserList(params)
        if (response.data && response.data.data) {
          this.userList = response.data.data.records
          this.total = response.data.data.total
        }
      } catch (error) {
        console.error('Fetch user list error:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async addUser(userData) {
      try {
        const response = await addUser(userData)
        return response
      } catch (error) {
        console.error('Add user error:', error)
        throw error
      }
    },
    
    async updateUserStatus(id, status) {
      try {
        const response = await updateUserStatus(id, status)
        return response
      } catch (error) {
        console.error('Update user status error:', error)
        throw error
      }
    },
    
    async deleteUser(id) {
      try {
        const response = await deleteUser(id)
        return response
      } catch (error) {
        console.error('Delete user error:', error)
        throw error
      }
    }
  }
}) 